<?php
	require_once APPPATH.'third_party/admin_controller'.EXT;
	class Product extends Admin_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
            $this->check_login_admin();
            $data = array();
            //$data['list_product'] = $this->product_model->list_product();
            $config['base_url'] =  base_url().'admin/product/index/';
			$uid =$this->uri->segment('4');            
			$config['per_page'] = '5'; 				
			$config['uri_segment'] = 4;
			//lay tong so trang
			$config['total_rows'] = $this->product_model->total();				
			$this->pagination->initialize($config);
			$data['page'] = $this->pagination->create_links();
			
			$data['list_product'] = $this->product_model->product_phantrang($uid,$config['per_page']);
            
            $this->load_render($this->load->view('admin/product/list',$data,TRUE));
        }
        public function add_update()
        {
            $this->check_login_admin();
            $data = array();
            $valid = $this->form_validation;
            $data['products_parent'] = $this->category_model->list_cats_children();
            $valid->set_rules('txt_name_product','Name Product','trim|required|xss_clean');
            $valid->set_rules('txt_description','Body','trim|required|xss_clean');      
            $valid->set_rules('txt_ms','Mã sản phẩm','trim|required|xss_clean');                
            $valid->set_rules('txt_url','Url','trim|required|xss_clean');
            $valid->set_rules('txt_id_cats','Danh mục','trim|required|xss_clean');
            $valid->set_rules('txt_price','Price','trim|required|xss_clean|numeric');           
            $id = $this->uri->segment('4');
			$image = $this->input->post('image');
			$image_thumb = $this->input->post('image_thumb');
            if($valid->run() == TRUE)
            {               
               $count = count($_POST['txt_vitri_show']);
               for($i = 0; $i < $count; $i++)
               {
                $vt[] = $_POST['txt_vitri_show'][$i];                
               }                            
               $name_product = $this->input->post('txt_name_product');
                $description = $this->input->post('txt_description');
                $meta_description = ($this->input->post('txt_meta_description')) ? $this->input->post('txt_meta_description') : "";
                $meta_keyword = ($this->input->post('txt_meta_keyword')) ? $this->input->post('txt_meta_keyword') : "";  
				$url = $this->input->post('txt_url');
                $price = $this->input->post('txt_price');
                $ms = $this->input->post('txt_ms');
                $weight = ($this->input->post('txt_weight')) ? $this->input->post('txt_weight') : 0;
                $height = ($this->input->post('txt_height')) ? $this->input->post('txt_height') : 0;
                $soluong = ($this->input->post('txt_soluong')) ? $this->input->post('txt_soluong') : 0;
                $sale_off = ($this->input->post('txt_saleoff')) ? $this->input->post('txt_saleoff') : 0;
                $time_sale_off = ($this->input->post('txt_time_sale_off')) ? $this->input->post('txt_time_sale_off') : 0;
                $active = $this->input->post('txt_active');
                $order_product = ($this->input->post('txt_order_product')) ? $this->input->post('txt_order_product') : 0;
                //$vitri_show = implode(',',$vt);
                $id_cats = $this->input->post('txt_id_cats');
                $vitri_show = $this->input->post('txt_vitri_show');
                $vitri_left = $this->input->post('txt_vt_left');
                $vitri_right = $this->input->post('txt_vt_right');
                $slidershow = $this->input->post('txt_vt_slidershow');
                $data = array(
				'name_product' => $name_product,
				'description'  => $description,
				'meta_keyword' => $meta_keyword,
				'meta_description' => $meta_description,
				'url'			=> $url,
				'price'			=> $price,
				'ms'			=> $ms,
				'weight'		=> $weight,
				'height'		=> $height,
				'soluong'		=> $soluong,
				'sale_off'		=> $sale_off,
				'time_sale_off'	=> $time_sale_off,
				'time_beign'	=> date('Y-m-d H:i:s'),
				'time_finish'	=> date('Y-m-d H:i:s'),				
				'active'		=> $active,
				'order_product' => $order_product,
				'vitri_show'	=> $vitri_show,
                'id_cats'	=> $id_cats,
                'vitri_left'	=> $vitri_left,
                'vitri_right'	=> $vitri_right,
                'slidershow'	=> $slidershow,					
			     );
				if($_FILES['p_image']['name']!='')
				{
					$config['upload_path'] = './upload/product/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;
					$config['max_size'] = 1024;
					$config['max_width'] = '1024';
					$config['max_height'] = '1024';
					$config['file_name'] = $_FILES['p_image']['name'];
					$this->load->library('upload', $config);
					if(!$this->upload->do_upload('p_image')) 
					{
					$data['error'] = $this->upload->display_error();
					} else {
					$upload = $this->upload->data();
					$data['image'] = 'upload/product/'.$upload['file_name'];
					$data['image_thumb'] = 'upload/product/'.$upload['raw_name'].'_thumb'.$upload['file_ext'];
					// tao image thumb
						$config['image_library'] = 'gd2';
						$config['source_image'] = $upload['full_path'];
						$config['thumb_maker'] = '_thumb';
						$config['create_thumb'] = TRUE;
						$config['maintain_ratio'] = FALSE;
						$config['quality'] = '100%';
						$config['width'] = '100';
						$config['height'] = '120';
						$config['new_image'] = $upload['file_name'];	
						$this->load->library('image_lib', $config);
						$this->image_lib->resize();
						$this->image_lib->clear();
						if($image !='' && $image_thumb !='')
						{
						@unlink($image);
						@unlink($image_thumb);
						}
					
						}	
				}
				else
				{
					if($image !='' && $image_thumb !='') {
					$data['image'] = $image;
					$data['image_thumb'] = $image_thumb;
					} else {
						$data['image'] = '';
						$data['image_thumb'] = '';
					}
				}
				
                
                if($id !='')
                {
                    $this->product_model->update_product($id,$data);
                    $data['error'] = "Cap nhat thanh cong";
                }
                else
                {
                    $this->product_model->add_product($data);
                    $data['error'] = "Them thanh cong";
                }
                redirect('admin/product/');
            }
			$data['uid'] = $id;
            $data['products'] = $this->product_model->get_id_product($id);
            
            $this->load_render($this->load->view('admin/product/add_update',$data,TRUE));
        }
        function delete_product()
        {
            $this->check_login_admin();
            $id = $this->uri->segment('4');
            if($this->product_model->delete_product($id))
            {
                redirect('admin/product');
            }
        }
        function change_update_active()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->input->get('id');
            $data['active'] = $this->input->get('active');
            if($this->product_model->change_update_active($id,$data))
            {
                $data['error'] = "Cập nhật thành công";
            }
            echo json_encode($data);
        }
    }
?>